var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({
  goto:app.goto,
  /**
   * 页面的初始数据
   */
  data: {
    notMrRoomsList:[],
    mrRoom:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHouseList();
  },

  getHouseList:function(){
    app.api.client_community_getAllBindRoom({
      token: wx.getStorageSync("token"),
      idCommunity: wx.getStorageSync("idCommunity"),
    }, (res) => {
      if (res.data.mrRoom == undefined) {
        this.setData({
          notMrRoomsList: res.data.notMrRooms,
          mrRoom: {
            idRoom:0,
            nameCommunity: wx.getStorageSync("nameCommunity")
          },
        })
      }
      else{
        this.setData({
          notMrRoomsList: res.data.notMrRooms,
          mrRoom: res.data.mrRoom,
        })
        wx.setStorageSync('mrRoom', res.data.mrRoom)

      }
    })

  },
  /**
   * 解除绑定房屋
  */
  relieveHourse:function(event){
    console.log(event);
    var _that = this;
    var url = app.globalData.domain + "/client/community/delBindRoom";
    var parm = {
      token: wx.getStorageSync("token"),
      idRoom: event.currentTarget.dataset.roomid
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("解除绑定房屋=>", res);
      Common.showToast("解绑成功");
      _that.getHouseList();
    }, error => {
      Common.showToast(error);
    });
  },

  //设置默认房屋
  setMrRoom: function (event){
    var _that = this;
    var url = app.globalData.domain + "/client/community/setMrRoom";
    var parm = {
      token: wx.getStorageSync("token"),
      idRoom: event.currentTarget.dataset.roomid
    }
    wxRequest.postRequest(url, parm).then(res => {
      Common.showToast("切换成功");
      console.log(res);
      this.getHouseList();
    }, error => {
      Common.showToast(error);
    });
  },
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})